package guowei.guliproduct.product.service.impl;

import guowei.guliproduct.product.dao.CategoryBrandRelationDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.guliproduct.product.dao.BrandDao;
import guowei.guliproduct.product.entity.BrandEntity;
import guowei.guliproduct.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(key != null){
            wrapper.eq("brand_id",key).or().like("name",key)
                    .or().like("descript",key).or().eq("first_letter",key);

            IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params),
                    new QueryWrapper<BrandEntity>()
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<BrandEntity> getalls() {
        List<BrandEntity> brandEntities = brandDao.getallBrands();
        return brandEntities;
    }

    @Transactional
    @Override
    public void updateDetails(BrandEntity brand) {
        this.updateById(brand);
        categoryBrandRelationDao.updateByBrandIdAndBrandName(brand.getBrandId(),brand.getName());
    }

    /**
     * 远程调用时间简短
     * @param brandIds
     * @return
     */
    @Cacheable(value = "brandIds",key = "'brandids'")
    @Override
    public List<BrandEntity> getByIds(List<Long> brandIds) {
        List<BrandEntity> brands = this.list(new QueryWrapper<BrandEntity>().in("brand_id", brandIds));
        return brands;
    }

}